package cn.loioi.multi.service.impl.uesr;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import cn.loioi.multi.common.BaseServiceImpl;
import cn.loioi.multi.mysql.been.SysResourceMapper;
import cn.loioi.multi.mysql.model.SysResource;
import cn.loioi.multi.service.user.SysResourceService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class SysResourceServiceImpl extends BaseServiceImpl<SysResource> implements SysResourceService {
	private @Resource SysResourceMapper sysResourceMapper;

	@Override
	public List<Map<String, Object>> selectRoleResource(Integer roleID) {
		return mapper().selectRoleResource(roleID);
	}

	@Override
	public int insertRoleResource(Integer roleID, JSONArray ids) {
		if (roleID == null) return 0;
		mapper().deleteRoleResource(roleID);
		if (ids == null || ids.size() <= 0) {
			return 0;
		} else {
			return mapper().insertRoleResource(roleID, ids.toArray(new Integer[] {}));
		}
	}

	@Override
	public List<SysResource> selectRootResource() {
		return selectResourceByParentid(0);
	}

	@Override
	public List<SysResource> selectResourceByParentid(int parentid) {
		Example example = new Example(SysResource.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("parentid", parentid);
		return mapper().selectByExample(example);
	}

	@Override
	public int insert(SysResource record) {
		record.setCreateTime(new Date());
		return super.insert(record);
	}

	@Override
	public int update(SysResource record) {
		record.setUpdateTime(new Date());
		return super.update(record);
	}

	@Override
	protected SysResourceMapper mapper() {
		return sysResourceMapper;
	}

	@Override
	protected Class<SysResource> getGenericSuperclass() {
		return SysResource.class;
	}
}
